<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/09/0009
 * Time: 15:50
 */

namespace app\sports_admin\model;


use think\Model;

class HomeworkComment extends Model
{
    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    protected $table = 'homework_comment';

    public function getContentsAttr($value)
    {
        return $value == strlen($value) > 20 ? mb_substr($value,0,20).'.....' : $value;
    }
    /*
 * 家庭作业评论列表
 * @param $homeworkId 家庭作业id
 * @param $page 第几页
 * @param $rows 每页显示的条数
 * @param $sort 要排序的字段
 * @param $sortOrder 升序/降序
 * @param $searchName 要检索的字段
 * @param $start 开始时间
 * @param $end 结束时间
 */
    public static function homeworkCommentList($page,$rows,$sort,$sortOrder,$searchName,$homeworkId,$start,$end)
    {
        $result = self::where(function($query)use($start,$end,$searchName){
            if($searchName){
                $query->where('name',$searchName);
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->where('homework_id',$homeworkId)->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::where(function($query)use($start,$end,$searchName){
            if($searchName){
                $query->where('name',$searchName);
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->where('homework_id',$homeworkId)->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
}